#!/usr/bin/env bash

# 配置文件
function frps_config() {
  # 将配置写进配置文件
  echo_sleep "开始写入配置文件……"

  cat >${applicationPath}/conf/frps.ini <<-EOF
[common]
bind_port = 7056
dashboard_addr = 0.0.0.0
dashboard_port = 7556
dashboard_user = admin
dashboard_pwd = 1234
token = djj4s-a8gis+s
EOF

  echo_sleep "配置文件写入完成！\n"
}

# 判断容器是否启动成功
function frps_successOrNot() {
  echo_sleep "docker run命令:\n $dockerRunCmd \n" 2
  if [[ -n $(docker ps -q -f "name=^$application$") ]]; then
    echo_red "$application 容器启动成功"
    echo -e "\t通讯端口号：$port"
    echo -e "\t控制台端口号：$dashboardPort"
    echo -e "\t控制台的用户名：$frpUser"
    echo -e "\t控制台的密码：$passwd"
    echo -e "\tToken：$token"
    echo -e "\t容器名称：$application"
    echo -e "\t本地映射路径：$applicationPath"
    echo -e "进入容器使用命令：docker exec -it $application /bin/bash"
  else
    log_error "$application 容器启动失败,请检查docker run命令是否有误,端口是否被占用"
  fi
}

# 安装Frps
function frps_install() {
  application="frps"                        #应用名称,docker ps中显示的名称
  image="snowdreamtech/frps"                #docker仓库中镜像名称
  applicationPath="/opt/frp/${application}" # 本地映射文件路径
  port="7056"                               # 通讯端口号
  dashboardPort="7556"                      # 控制台端口号
  frpUser="admin"                           # 控制台的用户名
  passwd="1234"                             # 控制台的用户名
  token="djj4s-a8gis+s"                     # 客户端连接时需要
  version="latest"                          # 镜像版本号,默认为最新版本
  dockerRunCmd=""                           # 最后执行的docker run命令

  echo -e "\n=========================开始安装Frp========================="

  echo_yellow "1.检测是否存在 ${application} 容器"
  docker_delExisting ${application}

  echo_yellow "2.开始拉取镜像文件"
  docker_pull

  echo_yellow "3.开始创建本地映射目录"
  create_file_path conf

  echo_yellow "4.开始创建配置文件"
  frps_config

  echo_yellow "5.创建并启动docker容器"
  port=$(read_input "请输入通讯端口(默认为 ${port})：" ${port})
  application=$(read_input "请输入容器的名称(默认为 ${application})：" ${application})
  frpUser=$(read_input "请输入控制台用户名(默认为 ${frpUser})：" ${frpUser})
  dashboardPort=$(read_input "请输入控制台密码(默认为 ${passwd})：" ${dashboardPort})
  token=$(read_input "请输入通讯token(默认为 ${token})：" ${token})

  config_add_or_update "bind_port = ${port}" "${applicationPath}/conf/frps.ini"
  config_add_or_update "dashboard_port = ${dashboardPort}" "${applicationPath}/conf/frps.ini"
  config_add_or_update "dashboard_pwd = ${passwd}" "${applicationPath}/conf/frps.ini"
  config_add_or_update "dashboard_user = ${frpUser}" "${applicationPath}/conf/frps.ini"
  config_add_or_update "token = ${token}" "${applicationPath}/conf/frps.ini"

  echo -e "${application}启动中,请等待......"

  dockerRunCmd="docker run --restart=always --privileged=true \
	--network host -d \
	-v /etc/localtime:/etc/localtime:ro \
	-v ${applicationPath}/conf/frps.ini:/etc/frp/frps.ini \
	--name $application -d $image:$version &> /dev/null"

  eval $dockerRunCmd

  echo_yellow "6.检测容器是否启动成功"
  frps_successOrNot

  echo -e "=========================安装Frp结束========================="
}

# 配置文件
function frpc_config() {
  # 将配置写进配置文件
  echo_sleep "开始写入配置文件……"

  cat >${applicationPath}/conf/frpc.ini <<-EOF
[common]
server_addr = 127.0.0.1
server_port= 7056
token = djj4s-a8gis+s

[web]
type = tcp
local_ip = 127.0.0.1
local_port = 80
remote_port = 8080
EOF

  echo_sleep "配置文件写入完成！\n"
}

# 判断容器是否启动成功
function frpc_successOrNot() {
  echo_sleep "docker run命令:\n $dockerRunCmd \n" 2
  if [[ -n $(docker ps -q -f "name=^$application$") ]]; then
    echo_red "$application 容器启动成功"
    echo -e "\t通讯端口号：$port"
    echo -e "\t服务端地址：$dashboardPort"
    echo -e "\tToken：$token"
    echo -e "\t容器名称：$application"
    echo -e "\t本地映射路径：$applicationPath"
    echo -e "进入容器使用命令：docker exec -it $application /bin/bash"
    echo -e "请前往${applicationPath}/conf/frpc.ini 配置映射设置"
  else
    log_error "$application 容器启动失败,请检查docker run命令是否有误,端口是否被占用"
  fi
}

# 安装Frpc
function frpc_install() {
  application="frpc"                        #应用名称,docker ps中显示的名称
  image="snowdreamtech/frpc"                #docker仓库中镜像名称
  applicationPath="/opt/frp/${application}" # 本地映射文件路径
  local servicePort="7056"                  # 通讯端口号
  local serviceIp="127.0.0.1"               # 服务器ip
  local serviceToken="djj4s-a8gis+s"        # 认证Toekn
  version="latest"                          # 镜像版本号,默认为最新版本
  dockerRunCmd=""                           # 最后执行的docker run命令

  echo -e "\n=========================开始安装Frp========================="

  echo_yellow "1.检测是否存在 ${application} 容器"
  docker_delExisting ${application}

  echo_yellow "2.开始拉取镜像文件"
  docker_pull

  echo_yellow "3.开始创建本地映射目录"
  create_file_path conf

  echo_yellow "4.开始创建配置文件"
  frpc_config

  echo_yellow "5.创建并启动docker容器"

  application=$(read_input "请输入容器的名称(默认为 ${application})：" ${application})
  serviceIp=$(read_input "请输入服务端的ip地址(默认为 ${serviceIp})：" ${serviceIp})
  servicePort=$(read_input "请输入通讯端口(默认为 ${servicePort})：" ${servicePort})
  serviceToken=$(read_input "请输入通讯token(默认为 ${serviceToken})：" ${serviceToken})

  config_add_or_update "server_addr = ${serviceIp}" "${applicationPath}/conf/frpc.ini"
  config_add_or_update "server_port = ${servicePort}" "${applicationPath}/conf/frpc.ini"
  config_add_or_update "token = ${serviceToken}" "${applicationPath}/conf/frpc.ini"

  echo -e "${application}启动中,请等待......"

  dockerRunCmd="docker run --restart=always --privileged=true \
	--network host -d \
	-v /etc/localtime:/etc/localtime:ro \
	-v ${applicationPath}/conf/frpc.ini:/etc/frp/frpc.ini \
	--name $application -d $image:$version &> /dev/null"

  eval $dockerRunCmd

  echo_yellow "6.检测容器是否启动成功"
  frpc_successOrNot

  echo -e "=========================安装Frp结束========================="
}

# 安装frp
function frp_install() {
  clear
  echo -e "(1) frps(服务端)             (2)frpc(客户端)"
  echo -e "(0) Back(返回上一层菜单)"

  read -r -p "请输入要安装服务的编号(select)：" input
  case $input in
  1)
    frps_install
    exit
    ;;
  2)
    frpc_install
    ;;
  0)
    echo -e "返回上一层菜单"
    docker_main
    ;;
  *)
    clear
    echo_red "请输入正确的编号，ctrl+c退出"
    sleep 1s
    docker_main
    ;;
  esac
}
